﻿using Auth.Application.IServices.Base;
using Auth.Dtos.Base;
using Auth.Entities.Base;
using Auth.IRepository;
using Auth.Models;
using Auth.Models.Base;
using AutoMapper;
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;

namespace Auth.Application.Services.Base
{
    public class UserService : IUserService
    {
        /// <summary>
        /// 工作单元
        /// </summary>
        private readonly IUnitOfWork UnitOfWork;

        /// <summary>
        /// AutoMapper
        /// </summary>
        private readonly IMapper _mapper;

        public UserService(IUnitOfWork unitOfWork, IMapper mapper)
        {
            UnitOfWork = unitOfWork;
            _mapper = mapper;
        }

        /// <summary>
        /// 新增
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>

        public async Task<Guid?> InsertAsync(UserDto dto)
        {
            User entity = _mapper.Map<User>(dto);

            // 判断Value是否已存在
            if (await UnitOfWork.Context.HasValueAsync<User>(nameof(entity.Name), entity.Name))
            {
                return null;
            }
            else
            {
                await UnitOfWork.Context.InsertAsync(entity);
                return entity.Id;
            }
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        public async Task<bool> DeleteAsync(UserDto dto)
        {
            User entity = _mapper.Map<User>(dto);
            bool result = await UnitOfWork.Context.DeleteAsync(entity);

            return result;
        }

        /// <summary>
        /// 更新
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        public async Task<bool> UpdateAsync(UserDto dto)
        {
            User entity = _mapper.Map<User>(dto);
            bool result = await UnitOfWork.Context.UpdateAsync(entity);

            return result;
        }

        /// <summary>
        /// 查询单条记录
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        public async Task<UserDto> GetAsync(Guid id)
        {
            User entity = await UnitOfWork.Context.GetAsync<User>(id);
            UserDto dto = _mapper.Map<UserDto>(entity);

            return dto;
        }

        /// <summary>
        /// 分页查询
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>

        public async Task<PagedList<UserDto>> GetPagedListAsync(UserModel model)
        {
            IEnumerable<User> entities = await UnitOfWork.Context.GetPagedListAsync<User, UserModel>(model.Pagination.Page, model.Pagination.ItemsPerPage, model);
            long count = await UnitOfWork.Context.CountAsync<User, UserModel>(model);

            IEnumerable<UserDto> list = _mapper.Map<IEnumerable<UserDto>>(entities);
            return new PagedList<UserDto> { List = list, Count = count };
        }

        /// <summary>
        /// 查询所有记录
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public async Task<IEnumerable<UserDto>> CountAsync(UserModel model)
        {
            IEnumerable<User> entities = await UnitOfWork.Context.GetListAsync<User, UserModel>(model);
            IEnumerable<UserDto> list = _mapper.Map<IEnumerable<UserDto>>(entities);

            return list;
        }
    }
}
